package com.dsbj.mp.icardservice.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.dsbj.mp.icardservice.config.intercept.SignInterceptor;
import com.dsbj.mp.icardservice.config.intercept.TokenInterceptor;
import com.netflix.discovery.converters.Auto;
import config.AppConfig;
import lib.MESSAGEXsend;
import lib.MessageSend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import utils.ConfigLoader;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Configuration
@ConfigurationProperties
public class MyWebAppConfigurer extends WebMvcConfigurerAdapter {

	@Autowired
	private TokenInterceptor tokenInterceptor;

	@Autowired
	private SignInterceptor signInterceptor;

	@Bean
	public HttpMessageConverter<String> responseBodyConverter() {
		StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
		return converter;
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/**").addResourceLocations("classpath:/");
		super.addResourceHandlers(registry);
	}

	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		super.configureMessageConverters(converters);
		//解决中文乱码
		//converters.add(responseBodyConverter());
		///去除MappingJackson2HttpMessageConverter
		Iterator<HttpMessageConverter<?>> iterator = converters.iterator();
		while (iterator.hasNext()) {
			if (iterator.next() instanceof MappingJackson2HttpMessageConverter) {
				iterator.remove();
			}
		}

		//解决 添加解决中文乱码后 上述配置之后，返回json数据直接报错 500：no convertter for return value of type
		//创建fastJson消息转换器

		FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();

		//创建配置类
		FastJsonConfig fastJsonConfig = new FastJsonConfig();

		//过滤并修改配置返回内容

		fastJsonConfig.setSerializerFeatures(
				SerializerFeature.WriteNullListAsEmpty,
				SerializerFeature.WriteNullBooleanAsFalse
		);

		//处理中文乱码问题

		List<MediaType> fastMediaTypes = new ArrayList<MediaType>();
		fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
		fastJsonConverter.setSupportedMediaTypes(fastMediaTypes);
		fastJsonConverter.setFastJsonConfig(fastJsonConfig);
		//将fastjson添加到视图消息转换器列表内
		converters.add(fastJsonConverter);
	}
    /*@Bean
    public MappingJackson2HttpMessageConverter messageConverter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(getObjectMapper());
        return converter;
    }
    @Bean
    public ObjectMapper getObjectMapper() {
        return new ObjectMapper();
    }*/

	@Bean("httpsRestTemplate")
	public RestTemplate httpsRestTemplate() {
		RestTemplate template = new RestTemplate(new HttpsClientRequestFactory());
		template.getMessageConverters().add(new FastJsonHttpMessageConverter());
		return template;
	}


	@Bean("submailConfig")
	public AppConfig getSubmaiCofig() {
		AppConfig config = ConfigLoader.load(ConfigLoader.ConfigType.Message);
		return config;
	}
	/**
	 * 拦截器配置。
	 *
	 * @param registry
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		//registry.addInterceptor(new SessionInteceptor()).addPathPatterns("/**").excludePathPatterns("/timeout").excludePathPatterns("/allowPower/**").excludePathPatterns("/images/**");
		//registry.addInterceptor(new SessionInteceptor()).addPathPatterns("/**").excludePathPatterns("/auth/**","/sa/**","/timeout","/allowPower/**","/quota/index/**","/quota/find/**","/images/**","/css/**","/js/**","/font/**","/data/**");

//		//签名拦截器配置
//		registry.addInterceptor(signInterceptor).addPathPatterns("/**").excludePathPatterns(
//				"/contr/**");

		//token拦截器配置
		registry.addInterceptor(tokenInterceptor).addPathPatterns("/**").excludePathPatterns(
				"/token/**",
				"/user/**",
				"/sa/**");


	}


}
